//
//  LYShowCallController.m
//  LYouSubscribeApp
//
//  Created by grx on 2018/12/25.
//  Copyright © 2018年 grx. All rights reserved.
//

#import "LYShowCallController.h"
#import "LJContactManager.h"
#import "LYouBuyMemberController.h"

@interface LYShowCallController (){
    NSString *thumImageUrl;
}

@property(nonatomic,strong) UIImageView *bgImageView;
@property(nonatomic,strong) UIButton *setCallButton;
@property(nonatomic,strong) UIButton *closeButton;
@property(nonatomic,strong) UIImageView *guaImageView;
@property(nonatomic,strong) UIImageView *jieImageView;

@end

@implementation LYShowCallController

- (instancetype)initThumbnail_url:(NSString *)url
{
    self = [super init];
    if (self) {
        thumImageUrl = url;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden = YES;
    /** 背景图片 */
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    [self.view addSubview:bgView];
    [bgView addSubview:self.bgImageView];
    /** 初始化返回按钮 */
    [self.view addSubview:self.closeButton];
    /** 初始化来电显示按钮 */
    [self.view addSubview:self.setCallButton];
    /** 初始化挂机图片 */
    [self.view addSubview:self.guaImageView];
    /** 初始化接听图片 */
    [self.view addSubview:self.jieImageView];
}

-(UIImageView *)bgImageView{
    if (!_bgImageView) {
        _bgImageView = [[UIImageView alloc]initWithFrame:self.view.bounds];
        _bgImageView.userInteractionEnabled = YES;
        _bgImageView.contentMode = UIViewContentModeScaleAspectFill;
        [self.bgImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", thumImageUrl]] placeholderImage:[UIImage imageNamed:@"placeholderimg"]];
    }
    return _bgImageView;
}

- (UIButton *)setCallButton {
    if (!_setCallButton) {
        _setCallButton = [UIButton new];
        _setCallButton.layer.masksToBounds = YES;
        [_setCallButton setBackgroundImage:[UIImage imageNamed:@"setCallButton"] forState:UIControlStateNormal];
        [_setCallButton setBackgroundImage:[UIImage imageNamed:@"setCallButton"] forState:UIControlStateHighlighted];
        _setCallButton.titleLabel.font = LYFont_Medium(14);
        [_setCallButton addTarget:self action:@selector(setCallButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [_setCallButton setTitle:@"设置为来电显示" forState:UIControlStateNormal];
        CGFloat safeBottem = MainWindows.safeAreaInsets.bottom;
        CGFloat hight = 68;
        if (safeBottem>0) {
            hight = 88;
        }
        _setCallButton.frame = CGRectMake((SCREEN_WIDTH-120)/2, SCREEN_HEIGHT-hight, 120, 40);
        _setCallButton.layer.cornerRadius = 20;

    }
    return _setCallButton;
}

-(UIButton *)closeButton{
    if (!_closeButton) {
        _closeButton = [UIButton new];
        _closeButton.layer.masksToBounds = YES;
        [_closeButton setBackgroundImage:[UIImage imageNamed:@"guanbi"] forState:UIControlStateNormal];
        [_closeButton setBackgroundImage:[UIImage imageNamed:@"guanbi"] forState:UIControlStateHighlighted];
        [_closeButton addTarget:self action:@selector(closeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        CGFloat safeBottem = MainWindows.safeAreaInsets.bottom;
        CGFloat hight = 62;
        if (safeBottem>0) {
            hight = 82;
        }
        _closeButton.frame = CGRectMake(SCREEN_WIDTH-60, SCREEN_HEIGHT-hight, 30, 30);
    }
    return _closeButton;
}

-(UIImageView *)guaImageView{
    if (!_guaImageView) {
        _guaImageView = [[UIImageView alloc]init];
        _guaImageView.userInteractionEnabled = YES;
        _guaImageView.image = [UIImage imageNamed:@"gua"];
        _guaImageView.frame = CGRectMake(SCREEN_WIDTH/2-120, SCREEN_HEIGHT-200, 70, 70);
    }
    return _guaImageView;
}

-(UIImageView *)jieImageView{
    if (!_jieImageView) {
        _jieImageView = [[UIImageView alloc]init];
        _jieImageView.userInteractionEnabled = YES;
        _jieImageView.image = [UIImage imageNamed:@"jie"];
        _jieImageView.frame = CGRectMake(SCREEN_WIDTH/2+50, SCREEN_HEIGHT-200, 70, 70);
    }
    return _jieImageView;
}

#pragma mark - 设置为来电显示
-(void)setCallButtonClick:(UIButton *)sender{
    /** 是否跳转到订购页面 */
    if ([ISHAVEBUYMEMBER isEqualToString:@"no"]) {
        /** 跳转到购买会员页 */
        LYouBuyMemberController *buyVC = [[LYouBuyMemberController alloc]init];
        [self.navigationController pushViewController:buyVC animated:YES];
        return;
    }
    /** 跳转通讯录 */
    [[LJContactManager sharedInstance] addToExistingContactsWithPhoneNum:thumImageUrl controller:self];
}

#pragma mark - 关闭按钮
-(void)closeButtonClick:(UIButton *)sender{
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
